<?php
namespace Admin\Controller;

class SettingController extends CommonController
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 查看系统变量
     * @return [type]
     */
    public function index()
    {
        $setting = setting([
            'red_send_min_money',
            'red_service_charge',
            'red_out_time',
            'red_voice_match_percent',
        ]);
        $this->assign('setting', $setting);

        $this->assign('bread_crumb', $bread_crumb = generate_bread_crumb([
            '系统设置',
            '系统信息',
        ]));
        $this->display();
    }

    /**
     * 开通城市
     * @return [type] [description]
     */
    public function city()
    {
        if (IS_GET) {
            $shop_id = I('get_id');
            $list    = M('shop_setting')
                ->where(['shop_id' => $shop_id])
                ->field('area')
                ->find();
            $this->assign('list', $list);
            $this->display();
        } elseif (IS_POST) {
            $city = I('post.content');
            $id   = M('shop_setting')
                ->where(['shop_id' => $shop_id])
                ->save(['area' => $city]);
            if (!$id) {
                $this->error('保存失败');
            } else {

                $this->success('保存成功');
            }
        }
    }

    /**
     * 电话设置
     */
    public function phone()
    {
     
        if (IS_GET) {
            $list = M('shop_setting')
                ->where(['shop_id' => $shop_id])
                ->field('area_number,mobile,telphone')
                ->find();
            $this->assign('list',$list);
            $this->display();
        }elseif (IS_POST) {
            $data = $_POST;
            $result = is_number($data['area_number']) && is_number($data['telphone']) && is_number($data['mobile']) ? 1 : 0 ;
            if (!$result) {
                $this->error('请输入有效数字');
            }

            $is_phone = is_phone($data['mobile']);
            if (!$is_phone) {
                $this->error('手机号码格式错误');
            }

            $tel = $data['area_number'] . '-' . $data['telphone'];
            $is_tel = is_tel($tel);
            if (!$is_tel) {
                $this->error('固定电话号码格式错误');
            }
            
            $id = M('shop_setting')->where(['shop_id' => $shop_id])->save($data);
            if (!$id) {
                $this->error('保存失败');
            }else{
                $this->success('保存成功');   
            }
        }
    }

    /**
     * 咨询设置
     */
    public function consult()
    {
        $this->display();
    }

    /**
     * 技师押金设置
     */
    public function cash()
    {
        $this->display();
    }
}
